package crawlingtosea.methods
{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import crawlingtosea.events.TickEvent;
	
	/**
	 * 时间基。当构造函数等于0时，启动进入帧事件。当非0时，则根据设置确定时间间隔。
	 * @author mengtianwxs
	 * @createTime 2013-6-9
	 */
	
	[Event(name="tick", type="crawlingtosea.events.TickEvent")]
	public class Tick extends EventDispatcher
	{
		private var _id:int;
		private var _counter:int=0;
		private var _s:Shape=new Shape();
		private var _t:int;
		
		public function Tick(tick:int=1)
		{
			if(tick!=0)
			{
				 _id=setInterval(closure,tick*1000);
			}
			else
			{
				_s.addEventListener(Event.ENTER_FRAME,onClipEvent);
			}
			this._t=tick;
				
		}
		
		protected function onClipEvent(event:Event):void
		{
			// TODO Auto-generated method stub
			dispatchEvent(new TickEvent(TickEvent.TICK,_counter++));
		}
		
		private function closure():void
		{
			// TODO Auto Generated method stub
			dispatchEvent(new TickEvent(TickEvent.TICK,_counter++));
			
		}
		
		public function stop():void
		{
			if(_t==0)
			{
				_s.removeEventListener(Event.ENTER_FRAME,onClipEvent);
			}
			else
			{
				clearInterval(_id);
			}
		}
		
	}
}